//
//  DetailViewController.m
//  LoveListen
//
//  Created by 王喆 on 15/7/2.
//  Copyright (c) 2015年 王喆. All rights reserved.
//

#import "DetailViewController.h"
#import "DetailModel.h"

#import "DetailTableViewCell.h"
#import "FirstDetailModel.h"
#import "MusicViewController.h"
#import "DBManager.h"
extern NSString * const kLZXFavorite;
extern NSString * const kLZXDownloads;
extern NSString * const kLZXBrowses;

#define kurl @"http://app.9nali.com/935/albums/%@?page_id=1&isAsc=true&device=iPhone&version=1.1.5"
@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)FirstDetailModel *detailModel;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
    [self addTaskWithUrl:[NSString stringWithFormat:kurl,self.albumId]];
   // [self addButton];
    NSLog(@"%@",self.albumId);
    BOOL isExist  = [[DBManager sharedManager] isExistAppForAppId:[NSString stringWithFormat:@"%@",self.albumId]];
    
    
    
    
    [self.dyButton addTarget:self action:@selector(dyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.dyButton setImage:[UIImage imageNamed:@"dyCell_fav_h"] forState:UIControlStateDisabled];
    [self.dyButton setImage:[UIImage imageNamed:@"dyCell_fav_n_meitu_1"] forState:UIControlStateNormal];
    if (isExist) {
        self.dyButton.enabled = NO;
    }else{
       self.dyButton.enabled = YES;
        
    }
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

-(void)dyButtonClick:(UIButton *)sender{
    UIButton *button = sender;
    button.enabled = NO;
    [[DBManager sharedManager] insertModel:self.model2 recordType:nil];


}
-(void)creatTableView{
    

    self.dataArr = [[NSMutableArray alloc]init];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -150,kScreenSize.width , kScreenSize.height+150) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 65;
    self.tableView.tableHeaderView = self.coverLargeImage;
    
    
    
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"detail"];
    //[self.coverLargeImage setDynamic:YES];
   // self.coverLargeImage.layer = ;
    [self.view addSubview:self.tableView];
    [self.tableView addSubview:self.lowImage];
    [self.tableView addSubview:self.coverSmallImage];
    [self.tableView addSubview:self.dyButton];
        [self.tableView addSubview:self.playButton];
    [self.tableView addSubview:self.tracksLabel];
    [self.tableView addSubview:self.nicknameLabel];
    [self.playButton addTarget:self action:@selector(playButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
   // [self.dyButton addTarget:self action:@selector(dyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}



#pragma mark - jianjie
-(void)addButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(self.tableView.tableHeaderView.frame.size.width-50,self.tableView.tableHeaderView.frame.size.height-50 , 30, 30) ;
    [button setBackgroundImage:[UIImage imageNamed:@"btn_user_info_n"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView.tableHeaderView addSubview:button];
    
    
    
}






-(void)addTaskWithUrl:(NSString *)url{
    
    _manager =[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"下载成功");
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *Fdict = dict[@"tracks"];
        NSDictionary *Sdict = dict[@"album"];
        NSMutableArray *arr = Fdict[@"list"];
        for (NSDictionary *dict2 in arr) {
            DetailModel *model = [[DetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dict2];
            self.model2 = [[DetailHeadModel alloc]init];
            [self.model2 setValuesForKeysWithDictionary:Sdict];
            self.navigationItem.title = self.model2.title;
            self.nicknameLabel.text = self.model2.nickname;
            self.tracksLabel.text  = [NSString stringWithFormat:@"节目数:%@",self.model2.tracks];
            [self.coverSmallImage sd_setImageWithURL:[NSURL URLWithString:self.model2.coverSmall] placeholderImage:[UIImage imageNamed:@"medium_head_male_default"]];
            [self.coverLargeImage sd_setImageWithURL:[NSURL URLWithString:self.model2.coverLarge] placeholderImage:[UIImage imageNamed:@"me"]];
            
            
           
            [self.dataArr addObject:model];
            [[DBManager sharedManager]insertModel:self.model2 recordType:kLZXBrowses];
            
            
            
        }
        
        [self.tableView reloadData];
        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
        
    }];
    
    
}

-(void)playButtonClick:(UIButton *)button{
//    MusicViewController *music = [[MusicViewController alloc]init];
//    NSLog(@"%ld",_dataArr.count);
//    
//   
//    
//    DetailModel *model = _dataArr[0];
//    music.playUrl64 = model.playUrl64;
//    music.coverLarge = model.coverLarge;
//    music.trackId = model.trackId;
//    [self.navigationController pushViewController:music animated:YES];
    
}




#pragma mark UITableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detail" forIndexPath:indexPath];
    DetailModel *model = self.dataArr[indexPath.row];
    [cell showDataWithModel:model indexPath:indexPath];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MusicViewController *music = [[MusicViewController alloc]init];
    DetailModel *model = _dataArr[indexPath.row];
    [music resiveArr:_dataArr index:(int)indexPath.row];
   
    music.playUrl64 = model.playUrl64;
    music.title = model.title;
    music.coverLarge = model.coverLarge;
    music.trackId = model.trackId;
   // music.isplaying = NO;
    
    
    
    
    [self.navigationController pushViewController:music animated:YES];
   //[self presentViewController:music animated:YES completion:nil];
    
    
    
}


@end
